import matplotlib as mpl
mpl.use("Agg")
import matplotlib.pyplot as plt     # 数学绘图库
import jieba               # 分词库
from wordcloud import WordCloud  # 词云库

filename = "./djzm.txt"
txt = open(filename, "r", encoding="utf8", errors="ignore").read()
words = jieba.cut(txt)
result = "/".join(words)  # 只有用"/"或" "等分割开的，才能用于构建词云图
# 常用的非人名的字词
no_name = ["说道", "出来", "如何", "跟着", "之后", "当真", "说话", "这里", "爹爹", "可是", "起来", "你们", "身上","两人","还是","这些","不会","自己","内力","怎么","南海","身子","脸上","原来","这么","弟子","众人", "这个","便是","倘若","突然","只是","不敢","他们","我们","见到","声音","心想","如此","只见","之中","不能","一个", "知道", "什么", "武功", "不是", "甚么", "一声", "咱们", "师父", "心中", "不知"]
# wordcloud配置
wc = WordCloud(
    font_path="C:\Windows\Fonts\msyhbd.ttc",  # 设置字体
    background_color='white',  # 背景颜色
    width=800, height=600,  # 设置宽高
    max_font_size=70, min_font_size=10,  # 字体最大/最小值
    stopwords=no_name,  # 设置停用词，不在词云图中显示
    max_words=2000,  # 设置最多显示的字数
    mode='RGBA'
)
wc.generate(result)
# 保存图片的名字
img_name = filename[:filename.rfind("."):] + "_词云图" + ".png"
# 生成图片
wc.to_file(img_name)
# 4、显示图片
plt.figure("词云图")  # 指定所绘图名称
plt.imshow(wc)       # 以图片的形式显示词云
plt.axis("off")      # 关闭图像坐标系
plt.show()